<template>
  <form-template
    class="form"
    v-model:form="form"
    labelWidth="7.25em"
    :options="options"
    :loading="loading"
    @submit="onSubmit"
  ></form-template>
</template>

<script setup>
  import {AuditStatus} from '@/utils/enum.js'
  const router = useRouter()
  const route = useRoute()
  const loading = ref(false)
  const detailId = ref()
  const form = ref({})
  const options = ref([
    {label: '原证照保管单位', key: 'origCustodyUnit', type: 'field', disabled: true},
    {label: '新证照保管单位', key: 'custodyUnit', type: 'field', disabled: true},
    {label: '证照号码', key: 'certNumber', type: 'field', disabled: true},
    {label: '转移事由', key: 'transferReason', type: 'textarea', disabled: true},
    {
      label: '审核结果',
      key: 'auditStatus',
      type: 'picker',
      columns: AuditStatus.filter(item => item.value !== 0),
      rules: [{required: true, message: '请选择审核结果'}],
    },
    {label: '审核意见', key: 'auditOpinion', type: 'textarea', rules: [{required: true, message: '请填写审核意见'}]},
  ])
  const onSubmit = () => {
    const params = {
      ...form.value,
      auditStatus: form.value.auditStatusValue,
    }
    delete params.id
    delete params.createBy
    delete params.createTime
    delete params.updateBy
    delete params.updateTime
    delete params.deleted
    delete params.auditStatusValue
    loading.value = true
    const loadingToast = showLoadingToast({
      duration: 500,
      message: '操作中...',
      forbidClick: true,
      loadingType: 'spinner',
    })
    $post('/api/v1/cert/transfer/record', params)
      .then(() => {
        showSuccessToast('操作成功')
        router.replace({path: '/'})
      })
      .finally(() => {
        loading.value = false
        loadingToast.close()
      })
  }
  const onLoad = id => {
    showLoadingToast({
      duration: 500,
      message: '加载中...',
      forbidClick: true,
      loadingType: 'spinner',
    })
    $get(`/api/v1/cert/transfer/record/${detailId.value}`)
      .then(res => {
        const {data} = res
        form.value = {
          ...data,
          auditStatus: undefined,
          auditOpinion: undefined,
        }
      })
      .finally(() => {
        closeToast()
      })
  }
  onMounted(() => {
    detailId.value = route.query?.id
    if (detailId.value) onLoad()
  })
</script>

<style scoped>
  .form {
    margin-top: 32px;
  }
</style>
